from django.db import models
from django.contrib.auth.models import AbstractBaseUser
import uuid

# Create your models here.

def generate_employee_id():
    return str(uuid.uuid4()).replace('-', '')[:20]  # 生成一个20位以内的唯一字符串

class User(models.Model):
    """用户表"""
    ROLE_CHOICES = [
        (0, '普通客服'),
        (1, '管理员'),
    ]
    
    COMMISSION_CALCULATION_CHOICES = [
        (0, '否'),
        (1, '是'),
    ]
    
    username = models.CharField(max_length=255, verbose_name='用户名')
    nickname = models.CharField(max_length=255, verbose_name='昵称')
    email = models.CharField(max_length=255, verbose_name='电子邮箱')
    phone_number = models.CharField(max_length=20, verbose_name='手机号码')
    pwd = models.CharField(max_length=20, verbose_name='密码')
    role = models.SmallIntegerField(choices=ROLE_CHOICES, verbose_name='角色')
    is_commission_calc = models.SmallIntegerField(choices=COMMISSION_CALCULATION_CHOICES, default=1, verbose_name='是否参与提成计算')
    employee_id = models.CharField(max_length=20, verbose_name='工号', unique=True)
    class Meta:
        verbose_name = '用户'
        verbose_name_plural = '用户'
        
    def __str__(self):
        return self.nickname
        
    def get_role_display(self):
        """
        获取角色的显示名称
        """
        return dict(self.ROLE_CHOICES).get(self.role, '未知角色')
        
    @property
    def is_authenticated(self):
        """
        为JWT认证提供支持
        """
        return True
    
    @property
    def is_anonymous(self):
        """
        为JWT认证提供支持
        """
        return False

class CommissionCategory(models.Model):
    """提成分类表"""
    commission_category = models.CharField(max_length=50, verbose_name='提成分类')
    commission_ratio = models.DecimalField(max_digits=5, decimal_places=2, verbose_name='提成比例')
    
    class Meta:
        verbose_name = '提成分类'
        verbose_name_plural = '提成分类'
        
    def __str__(self):
        return self.commission_category

class Attendance(models.Model):
    """考勤表"""
    nickname = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='昵称')
    creation_date = models.DateField(auto_now_add=True, verbose_name='创建日期')
    last_modification_date = models.DateTimeField(auto_now=True, verbose_name='最后修改日期')
    login_time = models.DateTimeField(verbose_name='上线时间')
    logout_time = models.DateTimeField(verbose_name='下线时间')
    online_duration = models.IntegerField(verbose_name='在线时长')
    duty_type = models.CharField(max_length=255, verbose_name='值班类型')
    duty_date = models.DateField(verbose_name='值班日期')
    hourly_wage = models.DecimalField(max_digits=10, decimal_places=1, verbose_name='时薪')
    
    class Meta:
        verbose_name = '考勤'
        verbose_name_plural = '考勤'
        
    def __str__(self):
        return f"{self.nickname} - {self.duty_date}"
